#include "curlWrapper.h"
#include <curlpp\curlpp.hpp>
#include <curlpp\Easy.hpp>
#include <curlpp\Options.hpp>
#include <boost\lexical_cast.hpp>
#include <iostream>


curlWrapper::curlWrapper(void)
{
	//curl "http://localhost:8080/workspace1?operation=updateGraph" -d "{\"an\":{\"0\":{\"x\":\"-1.35016698073779\",\"y\":\"-1.53300531273143\",\"z\":\"0\"}}}"
	this->url = "http://localhost:8080/workspace0?operation=updateGraph";
}


curlWrapper::~curlWrapper(void)
{
}

void curlWrapper::add_edge(int src,int tar)
{
	//CURL *curl;
	//curl = curl_easy_init();
	////string edge_id = boost::lexical_cast<std::string>(src)+boost::lexical_cast<std::string>(tar);
	//CURLcode res;
	//typedef std::basic_string<char> myString;
	//char src_string[10];
	//char tar_string[10];
	//itoa(src,src_string,10);
	//itoa(tar,tar_string,10);
	//char edge_id[1024] = "";
	//char ch[2] ="-";
	//strcat(edge_id,src_string);
	//strcat(edge_id,ch);
	//strcat(edge_id,tar_string);

	//static char errorBuffer[CURL_ERROR_SIZE];
	//if ( curl)
	//{
	//	curl_easy_setopt(curl,CURLOPT_ERRORBUFFER,errorBuffer);
	//	if (this->nodesadded.find(src) == this->nodesadded.end())
	//	{
	//		//myString url_to_send = this->url;	
	//		//string url_to_send = this->url;// + " {\"an\":{\""+ boost ::lexical_cast<std::string>(src) + "\":{}}}";
	//		curl_easy_setopt(curl,CURLOPT_URL,"http://localhost:8080/workspace0?operation=updateGraph");
	//		//myString postfield = "{\"an\":{\""+ boost ::lexical_cast<std::string>(src) + "\":{}}}";
	//		char SC[4096] = "{\"an\":{\"";
	//		char DC[4096] = "\":{}}}";
	//		
	//		
	//		strcat(SC,src_string);
	//		strcat(SC,DC);
	//		curl_easy_setopt(curl,CURLOPT_POSTFIELDS,SC);
	//		res=curl_easy_perform(curl);
	//		//cout <<res<<endl<<errorBuffer;
	//		this->nodesadded.insert(pair<int,bool>(src,true));
	//		
	//	}
	//	if ( this->nodesadded.find(tar) == this->nodesadded.end())
	//	{
	//		//string url_to_send = this->url + " {\"an\":{\""+ boost ::lexical_cast<std::string>(tar) + "\":{}}}";
	//		char SC[4096] = "{\"an\":{\"";
	//		char DC[4096] = "\":{}}}";
	//		char src_string[10];
	//		
	//		strcat(SC,tar_string);
	//		strcat(SC,DC);
	//		curl_easy_setopt(curl,CURLOPT_URL,"http://localhost:8080/workspace0?operation=updateGraph");
	//		curl_easy_setopt(curl,CURLOPT_POSTFIELDS,SC);
	//		curl_easy_perform(curl);
	//		this->nodesadded.insert(pair<int,bool>(tar,true));
	//	}
	//	//string url_add_edge = this->url+" {\"ae\":{\"" + edge_id +"\":{\"source\":\"" +  boost ::lexical_cast<std::string>(src) +"\",\"target\":\"" + boost ::lexical_cast<std::string>(tar) +"\",\"directed\":false}}}";
	//	curl_easy_setopt(curl,CURLOPT_URL,"http://localhost:8080/workspace0?operation=updateGraph");
	//	char SE[4096] = "{\"ae\":{\"";
	//	char sourcestr[1024] = "\":{\"source\":\"";
	//	char targetstr[1024] = "\",\"target\":\"";
	//	char directedstr[1024] = "\",\"directed\":false}}}";
	//	strcat(SE,edge_id);
	//	strcat(SE,sourcestr);
	//	strcat(SE,src_string);
	//	strcat(SE,targetstr);
	//	strcat(SE,tar_string);
	//	strcat(SE,directedstr);
	//	curl_easy_setopt(curl,CURLOPT_POSTFIELDS,SE);
	//	curl_easy_perform(curl);
	//}
	curlpp::Easy request;

	return;
}
void curlWrapper ::remove_edge(int src, int tar)
{
	CURL *curl;
	curl = curl_easy_init();
	//string edge_id = boost::lexical_cast<std::string>(src)+boost::lexical_cast<std::string>(tar);
	char src_string[10];
	char tar_string[10];
	itoa(src,src_string,10);
	itoa(tar,tar_string,10);
	char edge_id[1024] = "";
	char ch[2] ="-";
	strcat(edge_id,src_string);
	strcat(edge_id,ch);
	strcat(edge_id,tar_string);
	if ( curl)
	{
		//string url_remove_edge = this->url + " {\"de\": {\"" + edge_id+"\":{}}}";
		curl_easy_setopt(curl,CURLOPT_URL,"http://localhost:8080/workspace0?operation=updateGraph");
		char SE[4096] = "{\"de\": {\"";
		char DE[1024] = "\":{}}}";
		strcat(SE,edge_id);
		strcat(SE,DE);
		curl_easy_setopt(curl,CURLOPT_POSTFIELDS,SE);
		cout<<curl_easy_perform(curl);
	}
	return;
}

